#include <my_header.h>
/*接口名称 puts_client
 *接口功能简介 用于客户端，将要上传的文件发送到服务端
 *返回值的含义
 *return 0: 服务器文件发送到客户端成功
 *return -1: 文件名不匹配，下载失败
 *作者 周利通
 */
int gets(int netfd,//通信文件描述符
         const char *filename//下载文件名
         ){
    int access_ret=access(filename,F_OK);
    if(access_ret==-1){
        return access_ret;
    }
    train_t train;
    train.length=strlen(filename);
    memcpy(train.buff,filename,train.length);
    int send_filename_ret=send(netfd,&train,sizeof(train.length)+sizeof(train.taskType)+train.length,MSG_NOSIGNAL);
    ERROR_CHECK(send_filename_ret,-1,"send fliename");
    
    int fd=open(filename,O_RDWR);
    ERROR_CHECK(fd,-1,"open file");
    
    while(1){
        ssize_t read_ret=read(fd,train.buff,sizeof(train.buff));
        ERROR_CHECK(read_ret,-1,"read train.buff");
        
        train.length=read_ret;
        if(read_ret!=1000&&read_ret!=-1){
            printf("read_ret=%lu\n",read_ret);
        }
        int send_filedata_ret=send(netfd,train,sizeof(train.length)+sizeof(train.taskType)train.length,MSG_NOSIGNAL);
        ERROR_CHECK(send_filedata_ret,-1,"send filedata");
        
        if(read_ret==0){
            break;
        }
    }
    return 0;
}

